<?php
namespace Assist\Controller;

use Think\Upload;
use Think\Model;
class ImageController extends AssistController{
    public function index(){
        $brand = M('Car_brand');
        $where['logo'] = array('exp', 'is NULL');
        $menu = $brand->field('brand_id, brand_name')->where($where)->select();
        if (!isset($menu)) {
            $this->redirect('Image/show');
        }
        $menu1 = $menu;
        $menu2 = $menu;
        $menu3 = $menu;
        $this->assign('menu', $menu);
        $this->assign('menu1', $menu1);
        $this->assign('menu2', $menu2);
        $this->assign('menu3', $menu3);
        $this->display();
    }

    public function upload() {
        header('Content-Type: text/html; charset=utf8');
        $id['logofile1'] = $_POST['brand_id1'];
        $id['logofile2'] = $_POST['brand_id2'];
        $id['logofile3'] = $_POST['brand_id3'];
        $id['logofile4'] = $_POST['brand_id4'];

        foreach ($id as $key=>$value) {
            if (!isset($value)) {
                $this->error('未选择车辆品牌', U('Image/index'));
            }
        }
        exit;

        $upload = new Upload();
        $upload->maxSize = 3145728;
        $upload->exts = array('jpg', 'gif', 'png', 'jpeg', 'bmp');
        $upload->rootPath = './Uploads/Brand/';
        $upload->savePath = '';
        $upload->autoSub = false;

        $info = $upload->upload();
        if (!$info) {
            echo $upload->getError();
            exit;
        }

        $brand = D('Car_brand');
        foreach ($id as $key => $value) {
            $where['brand_id'] = $value;
            $logo = strchr($upload->rootPath, '/').$info[$key]['savepath'].$info[$key]['savename'];
            $data['logo'] = $logo;
            $brand->where($where)->save($data);
        }

        foreach ($id as $key => $value) {
            $where['brand_id'] = $value;
            $res = $brand->where($where)->select();
            echo '<img src="'.__ROOT__.$res[0]['logo'].'"/>';
        }

        $this->success('上传成功', U('Image/index'));
    }

    public function show() {
        header('Content-Type: text/html; charset=utf-8');
        $pic = M('Car_brand');
        $logo = $pic->field('brand_id,brand_name,logo')->select();
        $this->assign('logo', $logo);
        foreach ($logo as $key => $value) {
            if ($value['logo'][0] == '/') {
                $logo[$key]['logo'] = substr($value['logo'], 1);
            }
        }

        $this->display();
    }

    public function modify() {
        header('Content-Type: text/html; charset=utf8');
        if (!isset($_FILES['logoimg']['tmp_name'])) {
            $this->error('未选择更新文件');
        }

        if (!isset($_POST['brand_id']) || !isset($_POST['brand_name'])|| !isset($_POST['brand_logo']))
        {
            $this->error('文件信息错误');
        }

        $upload = new Upload();
        $upload->maxSize = 3145728;
        $upload->exts = array('jpg', 'gif', 'png', 'jpeg', 'bmp');
        $upload->rootPath = './Uploads/Brand/';
        $upload->savePath = '';
        $upload->autoSub = false;

        $info = $upload->upload();
        if (!$info) {
            echo $upload->getError();
            exit;
        }
        $brand = D('Car_brand');
        $where['brand_id'] = $_POST['brand_id'];
        $logo = strchr($upload->rootPath, '/').$info['logoimg']['savepath'].$info['logoimg']['savename'];
        $data['logo'] = $logo;
        $brand->where($where)->save($data);

        $this->success('上传成功', U('Image/index'));
    }

    public function up() {
        header('Content-Type: text/html; charset=utf8');
        if (!isset($_FILES['logoimg']['tmp_name'])) {
            $this->error('未选择更新文件');
        }

        $upload = new Upload();
        $upload->maxSize = 3145728;
        $upload->exts = array('jpg', 'gif', 'png', 'jpeg', 'bmp');
        $upload->rootPath = './Uploads/Brand/';
        $upload->savePath = '';
        $upload->autoSub = false;

        $info = $upload->upload();
        if (!$info) {
            echo $upload->getError();
            exit;
        }

        echo '<img src="'.__ROOT__.strchr($upload->rootPath, '/').$info['logoimg']['savepath'].$info['logoimg']['savename'].'"/>';
    }

    public function delete() {
       header('Content-Type: text/html; charset=utf-8');

    }

    protected function _del($path) {
        $path = realpath('.'.$path);
        if (file_exists($path)) {
            unlink($path);
        }
    }

    public function update() {
        $brand_id = $_POST['brand_id'];
        if (!isset($brand_id)) {
            $brand = M('Car_brand');
            $menu = $brand->field('brand_id, brand_name, logo')->select();
            $this->assign('menu', $menu);
            $this->display();
            exit;
        }

        if (isset($brand_id) && empty($_FILES['logoimg']['tmp_name'])) {
            $this->error('未选择车辆logo');
        }
        header('Content-Type: text/html; charset=utf8');

        echo '<pre>';
        var_dump($_FILES);
        echo '</pre>';
        exit;
        $brand = M('Car_brand');
        $map['brand_id'] = $brand_id;
        $res = $brand->where($map)->select();

        $upload = new Upload();
        $upload->maxSize = 3145728;
        $upload->exts = array('jpg', 'gif', 'png', 'jpeg', 'bmp');
        $upload->rootPath = './Uploads/Brand/';
        $upload->savePath = '';
        $upload->autoSub = false;
        $info = $upload->upload();
        if (!$info) {
            echo $upload->getError();
            exit;
        }

        $this->_del($res[0]['logo']);

        $logo = strchr($upload->rootPath, '/').$info['logoimg']['savepath'].$info['logoimg']['savename'];
        $data['logo'] = $logo;
        if ($brand->where($map)->save($data)) {
            $this->success('更新成功', U('Image/show'));
        } else {
            $this->error('更新失败');
        }

    }
}